package fr.jade.examples.fraclite.helloworld;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.deployment.local.api.IGenericFactoryWithModules;
import org.objectweb.fractal.util.Fractal;

import fr.jade.fraclite.util.FracLite;

public class HelloWorld {

	public static void main(final String[] args) throws Exception {

		Component rComp;

		Component boot = Fractal.getBootstrapComponent();
		TypeFactory tf = Fractal.getTypeFactory(boot);
		// type of root component
		ComponentType rType = tf.createFcType(new InterfaceType[] { tf
				.createFcItfType("m",
						"fr.jade.examples.fraclite.helloworld.Main", false,
						false, false) });
		// type of client component
		ComponentType cType = tf.createFcType(new InterfaceType[] {
				tf.createFcItfType("m",
						"fr.jade.examples.fraclite.helloworld.Main", false,
						false, false),
				tf.createFcItfType("s",
						"fr.jade.examples.fraclite.helloworld.itf.Service", true,
						false, false) });
		// type of server component
		ComponentType sType = tf.createFcType(new InterfaceType[] { tf
				.createFcItfType("s",
						"fr.jade.examples.fraclite.helloworld.itf.Service", false,
						false, false), });

		IGenericFactoryWithModules cf = (IGenericFactoryWithModules) Fractal
				.getGenericFactory(boot);

		// create root component
		rComp = cf.newFcInstance(rType, "composite", null);
		// create client component
		Component cComp = cf.newFcInstance(cType, "primitive",
				"fr.jade.examples.fraclite.helloworld.impl.ClientImpl",
				"clientModule-1.0.0");
		// create server component
		Component sComp = cf.newFcInstance(sType, "primitive",
				"fr.jade.examples.fraclite.helloworld.impl.ServerImpl",
				"serverModule-1.0.0");
		FracLite.getAttributeController(sComp).setAttribute("header", "> ");
		FracLite.getAttributeController(sComp).setAttribute("count", "2");

		// component naming
		Fractal.getNameController(cComp).setFcName("client");
		Fractal.getNameController(sComp).setFcName("server");
		Fractal.getNameController(rComp).setFcName("helloworld");

		// component assembly
		Fractal.getContentController(rComp).addFcSubComponent(cComp);
		Fractal.getContentController(rComp).addFcSubComponent(sComp);
		Fractal.getBindingController(rComp).bindFc("m",
				cComp.getFcInterface("m"));
		Fractal.getBindingController(cComp).bindFc("s",
				sComp.getFcInterface("s"));

		// start root component
		Fractal.getLifeCycleController(rComp).startFc();

		// call main method
		((Main) rComp.getFcInterface("m")).main(null);
	}

}
